import { Toast } from 'vant'
export default {
  data () {
    return {
      sendingSms: false,
      smsTime: 30
    }
  },
  methods: {
    onSendSms (phoneNum, callBack) {
      // 手机号校验
      if (!phoneNum) { Toast.fail('请填写 手机号'); return }
      if (!this.$util.validatePhoneNum(phoneNum)) { Toast.fail('手机号格式\n不正确'); return }
      // 发送倒计时逻辑
      if (!this.sendingSms) {
        this.$axios.post('/sms/send', { phoneNum: phoneNum }).then(res => {
          Toast.success('发送成功')
          // 开始倒计时
          this.sendingSms = true
          const maxTime = this.smsTime
          const clock = setInterval(() => {
            this.smsTime--
            if (this.smsTime <= 0) {
              clearInterval(clock)
              this.sendingSms = false
              this.smsTime = maxTime
            }
          }, 1000)
        }).catch(err => {
          Toast({ message: err.msg || '发送失败', position: 'bottom' })
        })
      }
    }
  }
}
